#!/bin/bash

export COLUMNS=320

while true; do
    _date1=`date "+%F"`
    _date2=`date -d"2 day ago" +%F`
    f="/data/metrics_${_date1}.log" 

    echo -----------------------`date "+%F %T"`--------------------------- >> $f
    echo -e '\n>>> top'>> $f
    top -b -c -n 1 > /tmp/abc
    cat /tmp/abc | head -n6 >> $f
    cat /tmp/abc | egrep 'worker.conf|mongod|mongos|proxy.conf' | grep -v grep >> $f

    echo -e '\n>>> free(g)'>> $f
    free -g >> $f

    echo -e '\n>>> iotop'>> $f 
    iotop -o -P -botq --iter=1 | head -n 10  >> $f 

    echo -e '\n>>> iostat'>> $f 
    iostat -d -x -y 1 2  >> $f 

    if [ -e "/data/top_$_date2.log" ]; then
        rm -f /data/metrics_${_date2}.log
    fi

    sleep 5
done
